<?
require_once ('../../models/Administrator.php');

if(isset($_POST['id']))
{
	$row = $_POST;
	$response = Administrator::Validate($row);
	
	if($response === true)
		if($row['id']==null)
			$response = Administrator::Insert($row);		
		else
			$response = Administrator::Update($row);
	
	if($response === true)
		header("Location: index.php?inserted=$row[id]");
}else{
	if(isset($_GET['id']))
		$row = Administrator::Get($_REQUEST['id']);
	else
		$row = Administrator::Blank();
}
?>


<!DOCTYPE html>
<html lang="en">
	<? include('../../inc/head.php'); ?>
	<body>
		<div>
			<? include('../../inc/nav.php'); ?>

			<div id="content">
				
				<? if(isset($response)): ?>
					<dl class="dl-horizontal error">
						<? foreach ($response as $key => $value) { ?>
							<dt><?=$key?></dt>
							<dd><?=$value?></dd>
						<? } ?>						
					</dl>
				<? endif; ?>
				<form class="form-horizontal" action="" method="post">
					<input type="hidden" name="id" value="<?=$row['idAdministrator']?>" />
					<? function RenderInput($propertyName, $inputtype){ ?>
						<? global $row, $response; ?>
						<div class="control-group">
							<label class="control-label" for="<?=$propertyName?>"><?=$propertyName?>:</label>
							<div class="controls">
								<input 	type="<?=$inputtype?>" name="<?=$propertyName?>" id="<?=$propertyName?>" value="<?=$row[$propertyName]?>"
									   	class="<?=isset($response[$propertyName]) ? 'error' : '' ?>"
								/>
								<? if(isset($response[$propertyName])): ?>
									<span class="error"><?=$response[$propertyName]?></span>
								<? endif; ?>
							</div>
						</div>
					<? } ?>
					<?
						RenderInput('Admin_Name', 'text');
						RenderInput('Admin_Email', 'text');
					?>				
					
					<div class="control-group">
						<div class="controls">
							<input type="submit" value="Save" class="btn btn-primary" />
						</div>
					</div>
			
				</form>
			</div>
			<? include('../../inc/footer.php'); ?>
		</div>
		<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
	</body>
</html>